package com.test.sdk.web.filter;


import com.test.sdk.common.exception.SdkException;
import com.test.sdk.common.pojo.Game;
import com.test.sdk.common.pojo.OnlineUser;
import com.test.sdk.common.util.DigestUtils;
import com.test.sdk.common.util.ErrorConstants;
import com.test.sdk.common.util.SignUtil;
import com.test.sdk.common.util.TicketUtil;
import com.test.sdk.service.service.OnlineService;
import com.test.sdk.web.cache.CPCache;
import com.test.sdk.web.cache.GameCache;
import com.test.sdk.web.exception.SdkExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter("*.html")
public class SignFilter implements Filter {
    //    @Autowired
//    private CPDAO cpDao;

    private static final String NEED_LOGIN_URL = "/user/.*";
    private static  final long TEN_MINUTE=1*60*1000;
    @Autowired
    private OnlineService onlineDAO;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("SingFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String sign = request.getParameter("sign");
        String cpId = request.getParameter("cpid");
        if (StringUtils.isEmpty(cpId)) {
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.PARAM_ERROR));
            return;
        }
        if (StringUtils.isEmpty(sign)) {
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SIGN_ERROR));
            return;
        }
        String gameId = request.getParameter("gid");
        if (StringUtils.isEmpty(gameId)) {
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.PARAM_ERROR));
            return;
        }
        int cp = Integer.parseInt(cpId);
        Game game = GameCache.getGame(Integer.parseInt(gameId));
        if (game == null || !game.getCpId().equals(cp)) {
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.GAME_NOT_EXIST));
            return;
        }
        // String secret=cpDao.getCpSecret(Integer.parseInt(cpId));

        String secret = CPCache.getCpSecret(cp);
        if (secret == null) {
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.PARAM_ERROR));
            return;
        }
        String signStr = SignUtil.getSign(request.getParameterMap());
        System.out.println(signStr);
        System.out.println(DigestUtils.getMD5(signStr + "&" + secret));
        if (!sign.equals(DigestUtils.getMD5(signStr + "&" + secret))) {//签名错误
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SIGN_ERROR));
            return;
        }
        String path = request.getServletPath();//当前请求路径
        if (path.matches(NEED_LOGIN_URL)) {
            String ticket = request.getParameter("ticket");
            if (StringUtils.isEmpty(ticket)) {
                SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.PARAM_ERROR));
                return;
            }
            long lastAct= TicketUtil.getLastActTime(ticket);
            if(System.currentTimeMillis()-lastAct>TEN_MINUTE){
                SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SESSION_TIMEOUT));
                return;
            }
            String decodeTicket= TicketUtil.decode(ticket);
            System.out.println("查询数据库");
            //可以替换成redis
            OnlineUser user = onlineDAO.getOnlineUser(decodeTicket);
            if (user == null) {
                SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SESSION_TIMEOUT));
                return;
            }
        }
        filterChain.doFilter(request, response);
    }

}
